﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CloudShot.Properties;
using CloudShot.Utils;

namespace CloudShot
{
	partial class AboutBox : Form
	{
		private const string EMAIL = "chill.hr@gmail.com";
		private const string HELP = @"http://cloudshot.codeplex.com/documentation";

		public AboutBox()
		{
			InitializeComponent();
			this.Text = String.Format("About {0}", AssemblyTitle);
			this.lblProductName.Text = AssemblyProduct;
			this.lblVersion.Text = String.Format("version {0}", AssemblyVersion);
			this.lblCreatedBy.Text = "Created by name1ess0ne";
			this.lnkMail.Text = EMAIL;

			//this.lblCompanyName.Text = AssemblyCompany;
			//this.txtBoxDescription.Text = AssemblyDescription;
			this.Icon = Resources.applicationIcon;

			toolTip.SetToolTip(lnkError, "Click to open " + AppHelper.ErrorFile);
		}

		#region Assembly Attribute Accessors

		public string AssemblyTitle
		{
			get
			{
				object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
				if (attributes.Length > 0)
				{
					AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
					if (titleAttribute.Title != "")
					{
						return titleAttribute.Title;
					}
				}
				return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
			}
		}

		public string AssemblyVersion
		{
			get { return Application.ProductVersion; }
		}

		public string AssemblyDescription
		{
			get
			{
				object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
				if (attributes.Length == 0)
				{
					return "";
				}
				return ((AssemblyDescriptionAttribute)attributes[0]).Description;
			}
		}

		public string AssemblyProduct
		{
			get
			{
				object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
				if (attributes.Length == 0)
				{
					return "";
				}
				return ((AssemblyProductAttribute)attributes[0]).Product;
			}
		}

		public string AssemblyCopyright
		{
			get
			{
				object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
				if (attributes.Length == 0)
				{
					return "";
				}
				return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
			}
		}

		public string AssemblyCompany
		{
			get
			{
				object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
				if (attributes.Length == 0)
				{
					return "";
				}
				return ((AssemblyCompanyAttribute)attributes[0]).Company;
			}
		}

		#endregion

		private void lnkError_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			string errorFile = AppHelper.ErrorFile;

			if (File.Exists(errorFile))
				Process.Start(errorFile);
			else
				MessageBox.Show("No errors found", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);

			this.Close();
		}

		private void lnkMail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (string.IsNullOrEmpty(EMAIL))
				return;

			Process.Start("mailto://" + EMAIL);
			this.Close();
		}

		private void lnkHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (string.IsNullOrEmpty(HELP))
				return;

			Process.Start(HELP);
			this.Close();
		}
	}
}
